package com.sy.cloud.wechat.service;

import com.sy.cloud.wechat.ConstantsPlaceholder;
import com.sy.cloud.wechat.WeChatConfig;
import com.sy.cloud.wechat.WeChatException;
import com.sy.cloud.wechat.entity.AccessToken;
import com.sy.cloud.wechat.entity.JsApiTicket;
import com.sy.cloud.wechat.entity.WeChatEntity;
import com.sy.cloud.wechat.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * Created by zhuwenquan on 2017-10-30.
 */
@Service
@Slf4j
public class ConfigService {
    public AccessToken getAccessToken(String appId, String secret) {

        log.info( "apiUrl0"+appId+"__"+secret );
        String apiUrl = WeChatConfig.getUrl("get.access_token")
                .replace(ConstantsPlaceholder.APPID, appId)
                .replace(ConstantsPlaceholder.APPSECRET, secret);
        log.info( "apiUrl"+apiUrl );
        AccessToken accessToken = HttpUtils.get(apiUrl, AccessToken.class);

        log.info( "访问结束" );
        if (accessToken.isSuccess()) {
            return accessToken;
        } else {
            throw new WeChatException("get accesstoken error " + accessToken.toErrorString());
        }
    }

    public boolean verifyAccessToken(String accessToken) {
        String apiUrl = WeChatConfig.getUrl("get.menu")
                .replace(ConstantsPlaceholder.ACCESS_TOKEN, accessToken);
        WeChatEntity entity = HttpUtils.get(apiUrl, WeChatEntity.class);
        if (entity.isSuccess() || "46003".equals(entity.getErrcode())) {
            return Boolean.TRUE;
        } else if ("40001".equals(entity.getErrcode())
                || "40014".equals(entity.getErrcode())
                || "42001".equals(entity.getErrcode())) {
            return false;
        } else {
            throw new WeChatException("verify accessToken error " + entity.toErrorString());
        }
    }

    public boolean createMenu(String menuJson, String accessToken) {
        String apiUrl = WeChatConfig.getUrl("create.menu").replace(ConstantsPlaceholder.ACCESS_TOKEN, accessToken);
        WeChatEntity entity = HttpUtils.post(menuJson, apiUrl, WeChatEntity.class);

        if (entity.isSuccess()) {
            return Boolean.TRUE;
        } else {
            log.info("create menu error " + entity.toErrorString());
            throw new WeChatException("create menu error " + entity.toErrorString());
        }
    }


    public boolean deleteAllMenu(String accessToken) {
        String apiUrl = WeChatConfig.getUrl("delete.menu").replace(ConstantsPlaceholder.ACCESS_TOKEN, accessToken);
        WeChatEntity entity = HttpUtils.get(apiUrl, WeChatEntity.class);

        if (entity.isSuccess()) {
            return Boolean.TRUE;
        } else {
            throw new WeChatException("delete menu error " + entity.toErrorString());
        }
    }

    public JsApiTicket getJsApiTicket(String accessToken){
        String apiUrl = WeChatConfig.getUrl("get.jsapi.ticket").replace(ConstantsPlaceholder.ACCESS_TOKEN, accessToken);
        JsApiTicket jsApiTicket = HttpUtils.get(apiUrl, JsApiTicket.class);

        if (jsApiTicket.isSuccess()) {
            return jsApiTicket;
        } else {
            throw new WeChatException("get accesstoken error " + jsApiTicket.toErrorString());
        }
    }
}
